package clientClasses;

import commonClasses.commonInterfaces.ProtocolIntf;
import commonClasses.Utils;
import commonClasses.commonInterfaces.ClientModelIntf;
import commonClasses.commonInterfaces.HostModelIntf;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 *
 * @author Pozytywny!
 */

/*
 * scenariusz:
 *      tworzysz nowy class protocol
 *      connect()
 *      następnie wybrana metoda
 */
public class ClientProtocol implements ProtocolIntf {

    private InetAddress _ip;
    private int _port;
    private Socket _socket;
    private DataInputStream _dis;
    private DataOutputStream _dos;
    private ClientModelIntf _clientModel = null;
    public Orders _orders = new Orders();
    private Thread _clientServiceThread = null;

    public ClientProtocol(InetAddress ip, int port) {

        this._ip = ip;
        this._port = port;
    }

    public DataInputStream getDIS() {
        return _dis;
    }

    public DataOutputStream getDOS() {
        return _dos;
    }

    public InetAddress getIP() {
        return _ip;
    }

    public int getPort() {
        return _port;
    }

    private void connect() {
        try {
            this._socket = new Socket(_ip, _port);
            this._dis = new DataInputStream(_socket.getInputStream());
            this._dos = new DataOutputStream(_socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getItems() {
        Utils.write("wrzucam order getItems");
        _orders.add(
                new InfoCarier(InfoCarier.infoTypeEnum.getItems, null));
    }

    public void getItemDetails(long itemID) {
        Utils.write("wrzucam order getItemDetails");
        ArrayList<Object> objectList = new ArrayList<Object>();
        objectList.add((Long) itemID);
        _orders.add(
                new InfoCarier(
                InfoCarier.infoTypeEnum.getItemDetails,
                objectList));

    }

    public void login() {
        _orders.add(new InfoCarier(InfoCarier.infoTypeEnum.login, null));
    }

    public void enterAuction(long itemID) {

        ArrayList<Object> objectList = new ArrayList<Object>();
        objectList.add((Long) itemID);

        _orders.add(
                new InfoCarier(
                InfoCarier.infoTypeEnum.enterAuction,
                objectList));
    }

    public void newBid(long itemID, long value) {

        ArrayList<Object> objectList = new ArrayList<Object>();
        objectList.add(itemID);
        objectList.add(value);

        _orders.add(
                new InfoCarier(
                InfoCarier.infoTypeEnum.newBid,
                objectList));
    }

    public ClientModelIntf getClientModel() {
        return _clientModel;
    }

    public HostModelIntf getHostModel() {
        throw new UnsupportedOperationException("To jest Client protocol," +
                " nie ma w nim hostModelIntf.");
    }

    public Orders getOrders() {
        return _orders;
    }

    public Socket getSocket() {
        return this._socket;
    }

    public static void main(String[] args) {
        try {
            ClientProtocol cp = new ClientProtocol(
                    Inet4Address.getByName("localhost"),
                    1337);

            cp.connect();

            ClientModel clientModel = new ClientModel();
            clientModel.setIdentifier("aaa");
            cp._clientModel = clientModel;

            ClientService cs = new ClientService(cp, cp._clientModel);

            cp._clientServiceThread = new Thread(cs);
            cp._clientServiceThread.start();

            cp.newBid(123, 456);

            System.out.println("koniec.");

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }
}
